package org.xxx.epms.result.controller;


import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xxx.epms.common.api.CommonResult;
import org.xxx.epms.result.dto.ProvinceAndCityParam;
import org.xxx.epms.result.models.GridCity;
import org.xxx.epms.result.models.GridProvince;
import org.xxx.epms.result.service.GridCityService;
import org.xxx.epms.result.service.GridProvinceService;


import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lb
 * @since 2024-06-08
 */
@RestController
@RequestMapping("/result/gridProvince")
public class GridProvinceController {

    @Autowired
    private GridProvinceService gridProvinceService;
    @Autowired
    private GridCityService gridCityService;

    @ApiOperation("根据id查询省份信息")
    @RequestMapping(value = "/getGridProvinceById", method = RequestMethod.GET)
    public CommonResult<GridProvince> getGridProvinceById(@RequestParam("provinceId") Integer provinceId){
        GridProvince gridProvince = gridProvinceService.getGridProvinceById(provinceId);
        if(gridProvince == null){
            return CommonResult.failed("查询失败");
        }else {
            return CommonResult.success(gridProvince);
        }
    }

    @ApiOperation("展示所有省份信息")
    @RequestMapping(value = "/getAllGridProvinces", method = RequestMethod.POST)
    public CommonResult<List<GridProvince>> getAllGridProvinces(){
        return CommonResult.success(gridProvinceService.getAllGridProvinces());
    }

    @ApiOperation("添加省份城市信息")
    @RequestMapping(value = "/addGridProvince", method = RequestMethod.POST)
    public CommonResult<GridProvince> addGridProvince(@RequestBody ProvinceAndCityParam provinceAndCityParam){
        //添加省份信息
        GridProvince gridProvince = new GridProvince();
        gridProvince.setProvinceName(provinceAndCityParam.getProvinceName());
        gridProvince.setProvinceId(provinceAndCityParam.getProvinceId());
        gridProvinceService.addGridProvince(gridProvince);

        //添加城市信息
        GridCity gridCity = new GridCity();
        gridCity.setCityName(provinceAndCityParam.getCityName());
        gridCity.setCityId(provinceAndCityParam.getCityId());
        gridCity.setProvinceId(provinceAndCityParam.getProvinceId());
        gridCity.setProvinceName(provinceAndCityParam.getProvinceName());
        gridCityService.addGridCity(gridCity);

        return CommonResult.success(gridProvince);
    }
}

